<?php
/**
 * Created by PhpStorm.
 * User: 魏爷
 * Date: 2016/4/21
 * Time: 20:31
 */

namespace Home\Controller;

class TradeController extends BaseController
{
    /**
     * 用户管理
     **/
    public function userManage()
    {
        $u_id=getUserId();
        $user_list=D('Trade')->getUserInfo($u_id);
        $this->assign('_userList', $user_list);
        $this->display('Trade/userManage');
    }
    /**
     * 认证信息
     **/
    public function attestationInfo()
    {
        $u_id=getUserId();
        $getTopicInfo=D('Topic')->getTopicInfo();
        $getUserInfoById=D('UserLogin')->getUserInfoById($u_id);
        $this->assign('_getTopicInfo', $getTopicInfo);
        $this->assign('_getUserInfoById', $getUserInfoById);
        $this->display('Trade/attestationInfo');
    }
    /**
     * 交易密码
     **/
    public function tradePassword()
    {
        $this->display('Trade/tradePassword');
    }
    /**
     * 登陆密码
     **/
    public function loginPassword()
    {
        $this->display('Trade/loginPassword');
    }


    /**
     * 交易密码修改
    **/
    public function BankPasswordSave()
    {
        $u_id=getUserId();
        $bank_pwd=I('bank_pwd');
        $bank_pwd_new=I('bank_pwd_new');
        $bank_repwd_new=I('bank_repwd_new');
        if (empty($bank_pwd)||empty($bank_pwd_new)||empty($bank_repwd_new)) {
            $this->error('信息不能为空', U('Home/Trade/tradePassword'));
        }
        if ($bank_pwd_new!=$bank_repwd_new) {
            $this->error('新生密码和重复密码不一致', U('Home/Trade/tradePassword'));
        }
        $bankPwd=D('UserLogin')->getBankPwd($u_id, $bank_pwd);
        if ($bankPwd==false) {
            $this->error('原始密码不一致', U('Home/Trade/tradePassword'));
        }
        $res=D('UserLogin')->buyBankPwdSave($u_id, $bank_pwd_new);
        if ($res==false) {
            $this->error('修改失败', U('Home/Trade/tradePassword'));
        } else {
            $this->success('修改成功', U('Home/Trade/tradePassword'));
        }
    }
    /**
     * 清空安全全问题
    **/
    public function delectContent()
    {
        $u_id=getUserId();
        $res=D('Content')->delectContent($u_id);
        if ($res==false) {
            $this->error('清空失败', null, true);
        } else {
            D('UserLogin')->setContentById($u_id);
            $this->success('清空成功', null, true);
        }
    }
    /**
     * 安全问题添加
    **/
    public function safetyAdd()
    {
        $u_id=getUserId();
        $topic_id=I('topic_id');
        $content_name=I('content_name');
        if (empty($topic_id)||empty($content_name)) {
            $this->error('信息为空', null, true);
        }
        $getContentNumber=D('UserLogin')->getContentNumber($u_id);
        if ($getContentNumber>3) {
            $this->error('问题完成', null, true);
        }
        $res=D('Content')->safetyAdd($u_id, $topic_id, $content_name);
        if ($res==false) {
            $this->error('设置失败', null, true);
        } else {
            D('UserLogin')->setContentNumber($u_id);
            $this->success('设置成功', null, true);
        }
    }
    /***用户管理修改***/
    public function userManageSave()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize=3145728 ;// 设置附件上传大小
        $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './';
        $upload->savePath ='./Public/Uploads/'; // 设置附件上传目录
        $upload->autoSub=false;
        $upload->saveName = array('uniqid','');
        $info   =   $upload->uploadOne($_FILES['u_pic']);
        if (empty($info)) {// 上传错误提示错误信息
            $u_name=I('u_name');
            $u_email=I('u_email');
            $u_age=I('u_age');
            $u_sex=I('u_sex');
            $u_tel=I('u_tel');
            $u_address=I('u_address');
            $u_postcode=I('u_postcode');
            if (empty($u_name)||empty($u_email)) {
                $this->error('请完善信息！');
            }
            $res=D('Trade')->userManageSave($u_name, $u_email, $u_age, $u_sex, $u_tel, $u_address, $u_postcode);
            if ($res==false) {
                $this->error('修改失败，请重新填写！');
            } else {
                $this->success('修改成功！', U('Home/Trade/userManage'));
            }
        } else {// 上传成功
            $pictrueName=$info['savename'];
            $u_name=I('u_name');
            $u_email=I('u_email');
            $u_age=I('u_age');
            $u_sex=I('u_sex');
            $u_tel=I('u_tel');
            $u_address=I('u_address');
            $u_postcode=I('u_postcode');
            if (empty($u_name)||empty($u_email)) {
                $this->error('请完善信息！');
            }
            $res=D('Trade')->userManageSave($u_name, $u_email, $u_age, $u_sex, $u_tel, $u_address, $u_postcode, $pictrueName);
            if ($res==false) {
                $this->error('修改失败，请重新填写！');
            } else {
                $this->success('修改成功！', U('Home/Trade/userManage'));
            }
        }
    }
    /**登陆密码修改**/
    public function loginPasswordSave()
    {
        $u_pwd=I('u_pwd');
        $u_pwd_new=I('u_pwd_new');
        $u_repwd_new=I('u_repwd_new');
        if (empty($u_pwd)||empty($u_pwd_new)||empty($u_repwd_new)) {
            $this->error('输入的信息不能为空！');
        }
        if ($u_pwd_new!=$u_repwd_new) {
            $this->error('新生密码与重复密码不一致，请重新输入！');
        }
        $u_id=getUserId();
        $u_info=D('Trade')->getUserInfo($u_id);
        if ($u_info['u_pwd']!=myMd5($u_pwd)) {
            $this->error('请输入正确的原始密码！');
        }
        $res=D('Trade')->loginPasswordSave($u_pwd_new);
        if ($res==false) {
            $this->error('修改失败，请重新输入！');
        } else {
            $this->success('修改成功', U('Home/Trade/loginPassword'));
        }
    }

    /**
     * 密码重置
    **/
    public function alertInfo()
    {
        $u_id=getUserId();
        $u_pwd=I('u_pwd');
        $res=D('UserLogin')->alertInfo($u_id, $u_pwd);
        if ($res==false) {
            $this->error('重置失败', null, true);
        } else {
            $this->success('重置成功', null, true);
        }
    }
    /**
     * 真实姓名录入
    **/
    public function realInfoSave()
    {
        $u_id=getUserId();
        $real_name=I('real_name');
        $real_number=I('real_number');
        if (empty($real_name)||empty($real_number)) {
            $this->error('信息不能为空', U('Home/Trade/attestationInfo'));
        }
        $realNumber=D('UserAdd')->getRealNumber($real_number);
        if ($realNumber==true) {
            $this->error('您的信息已进行实名认证，请您重新认证。。。。。', U('Home/Trade/attestationInfo'));
        }
        $checkStatus=D('UserAdd')->checkStatus($u_id);
        if ($checkStatus==1) {
            $res=D('UserAdd')->realInfoSave($u_id, $real_name, $real_number);
            if ($res==false) {
                $this->error('提交失败', U('Home/Trade/attestationInfo'));
            } else {
                $this->success('您的信息已提交，请耐心等待', U('Home/Trade/attestationInfo'));
            }
        }
        if ($checkStatus==3) {
            $this->success('实名信息待认证中。。。。。。。。。。。。');
        }
        if ($checkStatus==2) {
            $this->success('实名信息已认证。。。。。。。。。。。。。。');
        }
    }
    /**
     * 银行卡信息添加
    **/
    public function bankSave()
    {
        $u_id=getUserId();
        $bank=I('bank');
        $bank_number=I('bank_number');
        if (empty($bank)||empty($bank_number)) {
            $this->error('信息不能为空', U('Home/Trade/attestationInfo'));
        }
        $res=D('UserAdd')->bankSave($u_id, $bank, $bank_number);
        if ($res==false) {
            $this->error('银行卡绑定失败。。。。。。');
        } else {
            $this->success('银行卡绑定成功。。。。。。');
        }
    }
}
